package common.util.prop;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Properties;


public class PropUtils {
	private static final Logger logger = LoggerFactory.getLogger(PropUtils.class);

	private static final Properties properties=new Properties();

	static {
		loadFile("rsjConf.properties");
		/*# ==============文件上传下载配置 开始==============
		upDownLoadUrl=http://192.168.1.27:9095/file/getFileAccessToken
		upDownAppId=RSJ
		upDownApi=rsj_pub_file_token
		# ==============文件上传下载配置 结束==============
		# ==============http://192.168.1.27:8090================
		redisUrl=http://192.168.1.27:8090
		if(0 == properties.size()){
			properties.setProperty("upDownLoadUrl", "http://192.168.1.27:9095/file/getFileAccessToken");
			properties.setProperty("upDownAppId", "RSJ");
			properties.setProperty("upDownApi", "rsj_pub_file_token");
			properties.setProperty("redisUrl", "http://192.168.1.27:8090");
		}*/
	}

	public static Properties loadFile(String propFileName){

		InputStream in;
		try {
			in = getIs(propFileName);
			properties.load(in);
			in.close();
		} catch (Exception e) {
			System.out.println("文件名称：" + propFileName);
			e.printStackTrace();
		}

		logger.error("properties:::{}", JSON.toJSONString(properties));
		return properties;
	}

	public static InputStream getIs(String filePath) throws FileNotFoundException {
		File f = new File(filePath);
		InputStream inputStream = null;
		inputStream = (f.exists())?new FileInputStream(f):PropUtils.class.getClassLoader().getResourceAsStream(filePath);
		return inputStream;
	}

	public static String getProperties(String key){
		return (String) properties.get(key);
	}
}
